package com.zy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zy.dao.CheckItemDao;
import com.zy.entity.PageResult;
import com.zy.entity.QueryPageBean;
import com.zy.pojo.CheckItem;
import com.zy.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheackItemServiceImpl implements CheckItemService {
	@Autowired
	private CheckItemDao checkItemDao;

	@Override
	public void add(CheckItem checkItem) {
		checkItemDao.add(checkItem);
	}

	@Override
	public PageResult pageQuery(QueryPageBean queryPageBean) {
		//查询的页码、每个条数、查询条件
		Integer currentPage = queryPageBean.getCurrentPage();
		Integer pageSize = queryPageBean.getPageSize();
		String queryString = queryPageBean.getQueryString();
		//使用mybatis的分页插件(可以自动的帮我们在SQL预计上添加limit)
		PageHelper.startPage(currentPage,pageSize);

		//查询数据
		//(使用了分页插件，这里的返回值用page对象接收)---->继续看
		Page<CheckItem> pages = checkItemDao.selectByCondition(queryString);

		long total = pages.getTotal();
		List<CheckItem> result = pages.getResult();

		return new PageResult(total,result);
	}

	@Override
	public void delete(Integer id) {
		//先查询是否能删除----是否有关联项
		long count =checkItemDao.findCountByCheckItemId(id);
		if(count > 0){
			//如果有关联，抛出异常，不删除数据
			throw new RuntimeException("当前检查项被引用，不能删除");
		}

		checkItemDao.deleteById(id);
	}

	@Override
	public void edit(CheckItem checkItem) {
		checkItemDao.edit(checkItem);
	}

	@Override
	public List<CheckItem> findAll() {
		return checkItemDao.findAll();
	}

	@Override
	public List<CheckItem> findAllOrder(Integer id) {
		return checkItemDao.findAllOrder(id);
	}
}
